//
//  CZMainViewController.swift
//  Weibo28
//
//  Created by Apple on 16/5/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  添加子控制的两种方式
//        viewControllers
        //
//        addChildViewController(<#T##childController: UIViewController##UIViewController#>)
        
        //  创建自定义tabbar
        let czTabBar = CZTabBar()
        czTabBar.czDelegate = self
        //  使用[weak self] 闭包的循环引用
        czTabBar.composeButtonClousre = { [weak self] in
            print("我也哈哈")
            //  判断用户是否登陆
            if CZUserAccountViewModel.sharedUserAccount.isLogin {
                self?.showComposeVC()
            } else {
                SVProgressHUD.showErrorWithStatus("您未登陆,请先登录!")
            }
            
        }
        
        
        //  使用kvc的设置只读属性, 设置自定义tabbar
        setValue(czTabBar, forKey: "tabBar")
        
       // self.tabBar = czTabBar

       addChildViewController(CZHomeTableViewController(), title: "首页", imageName: "tabbar_home")
       addChildViewController(CZMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
       addChildViewController(CZDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
       addChildViewController(CZProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

  
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //  设置全局统一的tabbar的选中颜色, 这句代码执行的越早越好, 可以考虑放到AppDelegate
        //UITabBar.appearance().tintColor = UIColor.orangeColor()
        //  设置tabbar 的title和image
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //  修改统一文字
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
//        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
//        childController.tabBarItem.image = UIImage(named: "allproducts")
//        childController.tabBarItem.selectedImage = UIImage(named: "allproducts_s")?.imageWithRenderingMode(.AlwaysOriginal)
//        childController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        
        //  修改图片的渲染模式
       childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //  修改文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)

        //  添加子视图控制器
        
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
    }

    //  进入发微博界面
    private func showComposeVC() {
        
        let composeVc = CZComposeViewController()
        
        let comoposeNav = UINavigationController(rootViewController: composeVc)

        presentViewController(comoposeNav, animated: true, completion: nil)
    
    }
    
    

}

extension CZMainViewController: CZTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}



